package be.novelfaces.component.util.js;

import static be.novelfaces.component.util.js.JS.SCRIPT;
import static be.novelfaces.component.util.js.JS.TEXT_JAVASCRIPT;
import static be.novelfaces.component.util.js.JS.TYPE;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class JavaScriptWriter {
	private static class SingletonHolder {
		public static final JavaScriptWriter instance = new JavaScriptWriter();
	}

	public static JavaScriptWriter getInstance() {
		return SingletonHolder.instance;
	}

	private JavaScriptWriter() {
	}

	public JavaScriptWriter startElement()
			throws IOException {
		getResponseWriter().startElement(SCRIPT.getName(), null);
		getResponseWriter().writeAttribute(TYPE.getName(),
				TEXT_JAVASCRIPT.getName(), null);
		return this;
	}

	public void endElement() throws IOException {
		getResponseWriter().endElement(SCRIPT.getName());
	}

	public JavaSriptConstructorWriterBuilder getJavaSriptConstructorWriterBuilder() {
		return new JavaSriptConstructorWriterBuilder(this);
	}

	private ResponseWriter getResponseWriter() {
		return FacesContext.getCurrentInstance().getResponseWriter();
	}

}
